package co.yixiang.yshop.module.message.supply;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import co.yixiang.yshop.framework.common.exception.ErrorCode;
import co.yixiang.yshop.module.member.api.user.dto.WechatUserDTO;
import co.yixiang.yshop.module.member.dal.dataobject.user.MemberUserDO;
import co.yixiang.yshop.module.member.service.user.MemberUserService;
import co.yixiang.yshop.module.message.dal.dataobject.wechattemplate.WechatTemplateDO;
import co.yixiang.yshop.module.message.enums.WechatTempateEnum;
import co.yixiang.yshop.module.message.mq.message.WeixinNoticeMessage;
import co.yixiang.yshop.module.message.service.wechattemplate.WechatTemplateService;
import co.yixiang.yshop.module.order.api.productorder.ProductOrderApi;
import co.yixiang.yshop.module.order.api.productorder.dto.OrderDTO;
import co.yixiang.yshop.module.order.api.productorder.dto.StoreAfterSalesDTO;
import co.yixiang.yshop.module.order.enums.AfterSalesStatusEnum;
import co.yixiang.yshop.module.order.enums.AfterStatusEnum;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;

;

/**
 * 小程序订阅消息通知
 */
@Service
public class WeiXinSubscribeService {

    @Resource
    private MemberUserService userService;
    @Resource
    private WechatTemplateService wechatTemplateService;
    @Resource
    private WxMaService wxMaService;
    @Resource
    private ProductOrderApi productOrderApi;

    /**
     * 分销佣金通知
     *
     * weixinNoticeMessage 数据
     */
    public void distributionNotice(WeixinNoticeMessage weixinNoticeMessage) {
        String openid = this.getUserOpenid(weixinNoticeMessage.getUid());

        if (StrUtil.isBlank(openid)) {
            return;
        }
        String productNames = productOrderApi.getOrderProductNames(weixinNoticeMessage.getOrderId());
        Map<String, String> map = new HashMap<>();
        map.put("first", "您的账户金币发生变动，详情如下：");
        map.put("character_string3", weixinNoticeMessage.getOrderId());
        map.put("thing2", productNames);
        map.put("amount4", weixinNoticeMessage.getBrokeragePrice().toString());
        String tempId = this.getTempId(WechatTempateEnum.DISTRIBUTION_MONEY_SUCCESS.getValue());
        if (StrUtil.isNotBlank(tempId)) {
            this.sendSubscribeMsg(openid, tempId, "/views/account/balance/index", map);
        }
    }


    /**
     * 售后通知
     *
     * @param weixinNoticeMessage 数据
     */
    public void orderAfterNotice(WeixinNoticeMessage weixinNoticeMessage) {

        String openid = this.getUserOpenid(weixinNoticeMessage.getUid());
        if (StrUtil.isBlank(openid)) {
            return;
        }
        String productNames = productOrderApi.getOrderProductNames(weixinNoticeMessage.getOrderId());
        StoreAfterSalesDTO afterSalesDTO = productOrderApi.getOrderAfterInfo(weixinNoticeMessage.getOrderId());
        String status = "未知";
        if(AfterSalesStatusEnum.PENDING_REVIEW.getValue().equals(afterSalesDTO.getState())){
            status = "待审核";
        }else if(AfterSalesStatusEnum.AUDITED.getValue().equals(afterSalesDTO.getState())){
            status = "已审核";
        }else if(AfterSalesStatusEnum.REFUND_SUCCESS.getValue().equals(afterSalesDTO.getState())) {
            status = "已退款";
        }else if(AfterStatusEnum.STATE_2.getValue().equals(afterSalesDTO.getSalesState())) {
            status = "商家拒绝";
        }
        Map<String, String> map = new HashMap<>();
        map.put("character_string9", weixinNoticeMessage.getOrderId());
        map.put("thing7", productNames);
        map.put("time4", LocalDateTimeUtil.format(afterSalesDTO.getCreateTime(), DatePattern.CHINESE_DATE_TIME_PATTERN));
        map.put("phrase1", status);
        String tempId = this.getTempId(WechatTempateEnum.ORDER_AFTER_NOTICE.getValue());
        if (StrUtil.isNotBlank(tempId)) {
            this.sendSubscribeMsg(openid, tempId,
                    "/orderInfo/orderInfo?details=orderId=" + weixinNoticeMessage.getOrderId(), map);
        }
    }



    /**
     * 发货成功通知
     *
     * @param weixinNoticeMessage 数据
     */
    public void deliverySuccessNotice(WeixinNoticeMessage weixinNoticeMessage) {

        String openid = this.getUserOpenid(weixinNoticeMessage.getUid());

        if (StrUtil.isEmpty(openid)) {
            return;
        }
        //OrderDTO orderDTO = productOrderApi.getOrderInfo(weixinNoticeMessage.getOrderId());
       // System.out.println("orderDTO:"+ orderDTO);
        Map<String, String> map = new HashMap<>();
        map.put("character_string7", weixinNoticeMessage.getOrderId());
        map.put("phrase12", "已经发货");
        map.put("thing1", weixinNoticeMessage.getDeliveryName());
        map.put("character_string2", weixinNoticeMessage.getDeliveryId());
        String tempId = this.getTempId(WechatTempateEnum.DELIVERY_SUCCESS.getValue());
        if (StrUtil.isNotBlank(tempId)) {
            this.sendSubscribeMsg(openid, tempId, "orderInfo/orderInfo?details=orderId=" + weixinNoticeMessage.getOrderId(), map);
        }
    }


    /**
     * 构建小程序一次性订阅消息
     *
     * @param openId     单号
     * @param templateId 模板id
     * @param page       跳转页面
     * @param map        map内容
     */
    private void sendSubscribeMsg(String openId, String templateId, String page, Map<String, String> map) {
        WxMaSubscribeMessage wxMaSubscribeMessage = WxMaSubscribeMessage.builder()
                .toUser(openId)
                .templateId(templateId)
                .page(page)
                .build();
        map.forEach((k, v) -> {
            wxMaSubscribeMessage.addData(new WxMaSubscribeMessage.MsgData(k, v));
        });
        try {
            wxMaService.getMsgService().sendSubscribeMsg(wxMaSubscribeMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取模板消息id
     *
     * @param key 模板key
     * @return string
     */
    private String getTempId(String key) {
        WechatTemplateDO yxWechatTemplate = wechatTemplateService.lambdaQuery()
                .eq(WechatTemplateDO::getType, "subscribe")
                .eq(WechatTemplateDO::getTempkey, key)
                .one();
        if (yxWechatTemplate == null) {
            throw exception(new ErrorCode(9999999, "请后台配置key:" + key + "订阅消息id"));
        }
        return yxWechatTemplate.getTempid();
    }


    /**
     * 获取openid
     *
     * @param uid uid
     * @return String
     */
    private String getUserOpenid(Long uid) {
        MemberUserDO yxUser = userService.getById(uid);
        if (yxUser == null) {
            return "";
        }

        if (StrUtil.isBlank(yxUser.getRoutineOpenId())) {
            return "";
        }
        return yxUser.getRoutineOpenId();

    }
}
